package top.lshaci.learning.springboot.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import top.lshaci.learning.springboot.web.model.Person;

import java.util.Date;

/**
 * RespBodyController
 *
 * @author lshaci
 * @since 1.0.0
 */
@Controller
@RequestMapping("/resp")
public class RespBodyController {

    /**
     * 标有{@link ResponseBody}注解的返回值，由{@link RequestResponseBodyMethodProcessor}进行处理
     */
    @ResponseBody
    @GetMapping("/person")
    public Person person() {
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(20);
        person.setBirth(new Date());
        return person;
    }

    /**
     * 1、浏览器发请求直接返回 xml              [application/xml]  jacksonXmlConverter
     * 2、如果是ajax发请求，返回 json          [application/json]  jacksonJsonConverter
     * 3、如果硅谷app发请求，返回自定义协议数据  [application/x-guigu]  guiguConverter
     *      属性值1；属性值2；……
     *
     * 步骤：
     * 1、添加自定义的MessageConverter进系统底层
     * 2、系统底层就会统计出所有MessageConverter能操作哪些类型
     * 3、客户端内容协商[x-guigu --> guiguConverter]
     */
    @ResponseBody
    @GetMapping("/person1")
    public Person person1() {
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(20);
        person.setBirth(new Date());
        return person;
    }
}
